//
//  LikxViewController.m
//  CarTown
//
//  Created by xionghao on 15/9/1.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "LikxViewController.h"
#import "Puser.h"
#import "UserInfoUpdateResde.h"
#import "AppDelegate.h"
@interface LikxViewController ()

@property (weak, nonatomic) IBOutlet UITextField *qqtextf;
@property (weak, nonatomic) IBOutlet UITextField *wecattf;
@property (weak, nonatomic) IBOutlet UILabel *phonelable;
@property (weak, nonatomic) IBOutlet UITextField *phonenumber;

@end

@implementation LikxViewController

+(instancetype)Link{
    LikxViewController *vc=[[LikxViewController alloc]initWithNibName:NSStringFromClass([self class]) bundle:nil];
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"联系方式";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
    // Do any additional setup after loading the view from its nib.
    if ([self.usermode.power integerValue]<5) {
        self.phonenumber.text=self.usermode.user_mobile;
        self.phonenumber.enabled=NO;
        self.wecattf.text=self.usermode.user_weixin;
        self.qqtextf.text=self.usermode.user_qq;
       
    }else{
        self.phonenumber.text=self.usermode.com_ctphone;
        self.wecattf.text=self.usermode.com_weixin;
        self.qqtextf.text=self.usermode.com_qq;
        self.phonelable.text=@"联系电话";
        
    }
   
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)rightclick{
    if ([self.usermode.power integerValue]<5) {
        if (![self.qqtextf.text isEqualToString: self.usermode.user_qq]||![self.wecattf.text isEqualToString: self.usermode.user_weixin]) {
            
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"正在保存";
            
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            
            NSMutableDictionary *params=[NSMutableDictionary dictionary];
            params[@"user_type"]=@"person";
            params[@"user_id"]=_usermode.user_id;
            params[@"action"]=@"p_link_change";
            params[@"user_qq"]=self.qqtextf.text;
            params[@"user_weixin"]=self.wecattf.text;
           
            [PostUitls Post:userinfourl parameters:params success:^(id response) {
                
                NSDictionary *dict=response;
                UserInfoUpdateResde *resde=[UserInfoUpdateResde resdeWithDict:dict];
                if ([resde.state intValue]==400) {
                    hud.mode = MBProgressHUDModeText;
                    hud.labelText=@"保存成功";
                    [hud hide:YES afterDelay:1];
                    self.usermode.user_weixin=self.wecattf.text;
                    self.usermode.user_qq=self.qqtextf.text;
                    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
                    [delegate saveContext];
                    if (_block) {
                        _block(YES);
                    }
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    hud.mode = MBProgressHUDModeText;
                    hud.labelText=resde.msg;
                    [hud hide:YES afterDelay:1];
                }
                
                
            } failure:^(NSError *error) {
                hud.mode = MBProgressHUDModeText;
                hud.labelText=@"保存失败";
                [hud hide:YES afterDelay:1];
            }];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        if (![self.qqtextf.text isEqualToString: self.usermode.com_qq]||![self.wecattf.text isEqualToString: self.usermode.com_weixin]||![self.phonenumber.text isEqualToString:self.usermode.com_ctphone]) {
            
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"正在保存";
            
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            
            NSMutableDictionary *params=[NSMutableDictionary dictionary];
            params[@"user_type"]=@"company";
            params[@"com_id"]=_usermode.com_id;
            params[@"action"]=@"c_link_change";
            params[@"com_qq"]=self.qqtextf.text;
            params[@"com_weixin"]=self.wecattf.text;
            params[@"com_ctphone"]=self.phonenumber.text;
            [PostUitls Post:userinfourl parameters:params success:^(id response) {
                
                NSDictionary *dict=response;
                UserInfoUpdateResde *resde=[UserInfoUpdateResde resdeWithDict:dict];
                if ([resde.state intValue]==400) {
                    hud.mode = MBProgressHUDModeText;
                    hud.labelText=@"保存成功";
                    [hud hide:YES afterDelay:1];
                    self.usermode.user_weixin=self.wecattf.text;
                    self.usermode.user_qq=self.qqtextf.text;
                    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
                    [delegate saveContext];
                    if (_block) {
                        _block(YES);
                    }
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    hud.mode = MBProgressHUDModeText;
                    hud.labelText=resde.msg;
                    [hud hide:YES afterDelay:1];
                }
                
                
            } failure:^(NSError *error) {
                hud.mode = MBProgressHUDModeText;
                hud.labelText=@"保存失败";
                [hud hide:YES afterDelay:1];
            }];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
